<?php

class ProductObject extends Object
{
	public $price;
	public $stock_status;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Object the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{object}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return CMap::mergeArray(array(
									array('price, stock_status', 'required')),Object::extraRules());
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return CMap::mergeArray(array(),Object::extraRelationships());
	}

     /**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return CMap::mergeArray(array(
									'price'=> t('Price'),
									'stock_status'=>t('Stock Status')
								),Object::extraLabel());
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		return Object::extraSearch($this);
	}
        
    protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{				
				$this->object_type='product';
                Object::extraBeforeSave('create',$this);
                                
			} else {
				Object::extraBeforeSave('update',$this);
												
			}
			return true;
		}
		else
			return false;
	}
        
	protected function afterSave()
	{
		parent::afterSave();
		if($this->isNewRecord){
			Object::saveMetaValue('price', $this->price, $this, true);
			Object::saveMetaValue('stock_status', $this->stock_status, $this, true);
		}
		else {
			Object::saveMetaValue('price', $this->price, $this, false);
			Object::saveMetaValue('stock_status', $this->stock_status, $this, false);
		}
	}
	
        public static function Resources(){

        
              return CMap::mergeArray(Object::Resources(),
				array(                      		   	                  
					'video'=>array('type'=>'video',
		              'name'=>'Video',
		              'maxSize'=>ConstantDefine::UPLOAD_MAX_SIZE, 
		              'minSize'=>ConstantDefine::UPLOAD_MIN_SIZE,
		              'max'=>1,
		              'allow'=>array('flv',
		                             'mp4',)),
					'thumbnail'=>array('type'=>'thumbnail',
		              'name'=>'Thumbnail',
		              'maxSize'=>ConstantDefine::UPLOAD_MAX_SIZE, 
		              'minSize'=>ConstantDefine::UPLOAD_MIN_SIZE,
		              'max'=>5,
		              'allow'=>array('jpeg',
		                             'jpg',
		                             'gif',
		                             'png')))
				);

        }
        
        public static function Permissions(){
              return Object::Permissions();
        }
        
}